
import javax.microedition.lcdui.*;
import javax.microedition.m3g.Group;
import javax.microedition.midlet.MIDlet;

/**
 * The alert demo displays a list of alerts that will be displayed once the
 * user clicks a list item. These alerts try to present the full range of
 * alert types supported in MIDP.
 *
 * @version 2.0
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
    private static final Command CMD_Restart = new Command("Restart", Command.OK, 1);
    private static final Command CMD_NEXT = new Command("NEXT", Command.OK, 1);
    private Display display;
    private Form mainForm;
    private int correct = 0;
    private int index = 0;
    String[] stringArray1 = {"Answer A", "Answer B", "Cor", "Answer D"};
    String[] stringArray2 = {"Answer A", "Answer B", "Answer C", "Cor"};
    String[] stringArray3 = {"Answer A", "Cor", "Answer C", "Answer D"};
    Image[] imageArray = null;

    // create the list of choice groups.
    ChoiceGroup[] groups = {
        new ChoiceGroup("Qestion1", ChoiceGroup.EXCLUSIVE, stringArray1, imageArray),
        new ChoiceGroup("Qeqstion2", ChoiceGroup.EXCLUSIVE, stringArray2, imageArray),
        new ChoiceGroup("Qestion3", ChoiceGroup.EXCLUSIVE, stringArray3, imageArray)
    };

    public void startApp() {

        display = Display.getDisplay(this);

        mainForm = new Form("Test");
       
        if (index < 3) {

            mainForm.append("Please answer the question");
            groups[index].setSelectedIndex(0, true);
            mainForm.append(groups[index]);
            index++;
            mainForm.addCommand(CMD_NEXT);
        } else {
               if (groups[0].isSelected(2)){
                   correct++;
                }
               if (groups[1].isSelected(3)){
                   correct++;
                }
               if (groups[2].isSelected(1)){
                   correct++;
                }
               mainForm.append("Correct answers: " + correct);
               mainForm.addCommand(CMD_Restart);
            }

        mainForm.addCommand(CMD_EXIT);
        mainForm.setCommandListener(this);

        display.setCurrent(mainForm);
    }

    public void commandAction(Command c, Displayable d) {

        if (c == CMD_EXIT) {
            destroyApp(false);
            notifyDestroyed();
        }

        if (c == CMD_NEXT) {
            mainForm.deleteAll();
            startApp();
        }
        if (c == CMD_Restart) {
            index = 0;
            correct = 0;
            mainForm.deleteAll();
            startApp();
        }
    }

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
    }
}
